package zhuyun.code.launcheractivity.view

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import zhuyun.code.launcheractivity.view.listener.PermissionListener

open class BaseActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    fun<T> startActivity(activity: Class<T>){
        startActivity(Intent(this,activity))
    }

    fun<T> startActivityForResult(activity: Class<T>,requestCode:Int){
        startActivityForResult(Intent(this,activity),requestCode)
    }

    protected fun backgroundAlpha(bgAlpha: Float) {
        val lp = window.attributes
        lp.alpha = bgAlpha //0.0-1.0
        window.attributes = lp
    }

    private var showFragment:Fragment? = null

    protected fun showFragment(resId:Int, fragment: Fragment){
        val fragmentTransaction = supportFragmentManager.beginTransaction()
        showFragment?.let { fragmentTransaction.hide(it) }
        val mFragment = supportFragmentManager.findFragmentByTag(fragment.javaClass.name)
        if(mFragment != null){
            mFragment?.let {
                fragmentTransaction.show(it)
                showFragment = mFragment
            }
        } else {
            fragmentTransaction.add(resId,fragment,fragment.javaClass.name)
        }
        fragmentTransaction.commitAllowingStateLoss()
    }

    private var mPermissionListener:PermissionListener? = null
    private val CODE_REQUEST_PERMISSION = 1

    fun requestPermissions(permissions:Array<String>,listener:PermissionListener){
        mPermissionListener = listener
        val permissionList = arrayListOf<String>()
        permissions.forEach {
            if(ContextCompat.checkSelfPermission(this,it)!=PackageManager.PERMISSION_GRANTED){
                permissionList.add(it)
            }
        }
        if(permissionList.isNotEmpty()){
            ActivityCompat.requestPermissions(this,permissionList.toArray(Array<String>(permissionList.size) { _->""}),CODE_REQUEST_PERMISSION)
        } else {
            mPermissionListener?.onGranted()
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when(requestCode){
            CODE_REQUEST_PERMISSION->{
                if(grantResults.isNotEmpty()){
                    val deniedPermissions = arrayListOf<String>()
                    for(i in grantResults.indices){
                        val result = grantResults[i]
                        if(result != PackageManager.PERMISSION_GRANTED){
                            deniedPermissions.add(permissions[i])
                        }
                    }
                    if(deniedPermissions.isEmpty()){
                        mPermissionListener?.onGranted()
                    } else {
                        mPermissionListener?.onDenied(deniedPermissions)
                    }
                }
            }

        }
    }
}
